package com.yoda.mall.admin.service;

import com.yoda.common.Status;
import com.yoda.common.exception.BusinessException;
import com.yoda.common.exception.ErrorResult;
import com.yoda.mall.api.HomeNewProductApi;
import com.yoda.mall.api.HomeRecommendProductApi;
import com.yoda.mall.api.ProductApi;
import com.yoda.mall.model.dto.HomeBrandDto;
import com.yoda.mall.model.dto.HomeProductDto;
import com.yoda.mall.model.dto.ProductDetailDto;
import com.yoda.mall.model.dto.ProductDto;
import com.yoda.mall.model.entity.pms.Product;
import com.yoda.mall.model.entity.sms.HomeBrand;
import com.yoda.mall.model.entity.sms.HomeNewProduct;
import com.yoda.mall.model.entity.sms.HomeRecommendProduct;
import com.yoda.mall.model.vo.PageResult;
import com.yoda.mall.model.vo.ProductDetailVo;
import com.yoda.mall.model.vo.R;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author shawn
 * @date 2022年 12月 17日 0:55
 */
@Service
public class ProductService {

    @DubboReference
    private ProductApi productApi;

    @DubboReference
    private HomeNewProductApi homeNewProductApi;

    @DubboReference
    private HomeRecommendProductApi homeRecommendProductApi;



    public PageResult<Product> selectByPage(ProductDto dto) {
       return productApi.selectByPage(dto);
    }


    public Integer createNewProduct(List<HomeProductDto> homeProductList) {
        if (homeProductList.size()==0){
            return 0;
        }
        List<HomeNewProduct> homeNewProducts = homeProductList.stream().map(HomeProductDto::initHomeNewProduct).collect(Collectors.toList());
        return homeNewProductApi.createNewProduct(homeNewProducts);
    }

    public Integer createRecommendProduct(List<HomeProductDto> homeProductList) {
        if (homeProductList.size()==0){
            return 0;
        }
        List<HomeRecommendProduct> homeRecommendProducts = homeProductList.stream().map(HomeProductDto::initHomeRecommendProduct).collect(Collectors.toList());
        return homeRecommendProductApi.createRecommendProduct(homeRecommendProducts);
    }

    public ProductDetailVo selectProductDetailById(Long productId) {
        return productApi.selectProductDetailById(productId);
    }

    public Integer createProduct(ProductDetailDto productParam) {
        return productApi.createProduct(productParam);
    }

    /**
     * 改变状态
     *
     * @param ids             id
     * @param status 状态
     * @param status          状态
     * @return {@link Integer}
     */
    public Integer changeStatus(List<Long> ids, Integer status, String type) {
        List<Product> products;
        switch (type){
            case "DELETE_STATUS": products = ids.stream().map(id->{
                Product product = new Product();
                product.setId(id);
                product.setDeleteStatus(status);
                return product;
            }).collect(Collectors.toList());
            break;

            case "NEW_PRODUCT_STATUS": products = ids.stream().map(id->{
                Product product = new Product();
                product.setId(id);
                product.setNewStatus(status);
                return product;
            }).collect(Collectors.toList());
            break;

            case "SOLD_STATUS": products = ids.stream().map(id->{
                Product product = new Product();
                product.setId(id);
                product.setPublishStatus(status);
                return product;
            }).collect(Collectors.toList());
            break;

            case "RECOMMEND_STATUS": products = ids.stream().map(id->{
                Product product = new Product();
                product.setId(id);
                product.setRecommandStatus(status);
                return product;
            }).collect(Collectors.toList());
            break;

            case "VERIFY_STATUS": products = ids.stream().map(id->{
                Product product = new Product();
                product.setId(id);
                product.setVerifyStatus(status);
                return product;
            }).collect(Collectors.toList());
            break;
            default:
                throw new BusinessException(ErrorResult.builder().errCode(Status.NOT_FOND.getCode()).errMessage("未知状态!").build());
        }
        return productApi.updateListById(products);
    }

    public Integer updateProduct(ProductDetailDto dto) {
       return productApi.updateProduct(dto);
    }
}
